/*===== code build by mstore0.1.3 =====*/

/*===== mstoreDev/build#compare:0 =====*/

$$('compare', function(){
    
    var $codeCompare;
    var $compareBox;
    
    $$(function($it){
        
        $it.click({
            closeCompare: function(){
                $$.compare.close();
            },
            submitImg: function(){
                $$.fileRes.getCurrent().submit();
            },
            submitCode: function(){
                $$.fileRes.getCurrent().submit('compareCode');
            },
            prevDiff: function(){
                $codeCompare.mergely('scrollToDiff', 'prev');
            },
            nextDiff: function(){
                $codeCompare.mergely('scrollToDiff', 'next');
            }
        });
        
        $it.extend({
            domready: function(){
                $it.initCompare();
            },
            initCompare: function(){
                $compareBox = $('#compareBox');
                $codeCompare = $('#editorHandle');
                $codeCompare.mergely({
                    height: 'auto',
                    width: 'auto',
                    cmsettings: {
                        readOnly: false,
                        lineNumbers: true,
                        theme: 'ttcn',
                        mode: 'javascript',
                        foldGutter: true,
                        lineWrapping: true,
                        fixedGutter: !true
                    }
                });
                $codeCompare.mergely('resize');
            }
        });
        
    });
    
    function compare(){
        
    }
    
    compare.close = function(){
        $compareBox.removeClass();
        $('html, body').removeClass('hideScroll');
    };
    
    compare.showCode = function(newCode, oldCode, dataMode){
        $compareBox.removeClass().addClass('showCodeBox');
        $('html, body').addClass('hideScroll');
        $codeCompare.mergely('options', {
            height: 'auto',
            width: 'auto',
            cmsettings: {
                //readOnly: true,
                lineNumbers: true,
                theme: 'ttcn',
                mode: ({
                    js: 'javascript',
                    css: 'css',
                    ftl: 'htmlmixed',
                    end: null
                })[dataMode],
                foldGutter: true,
                lineWrapping: true,
                fixedGutter: true
            }
        });
        $codeCompare.mergely('lhs', newCode);
        $codeCompare.mergely('rhs', oldCode || '');
    };
    
    compare.showImg = function(newImg, oldImg){
        $compareBox.find('.leftImgBox').html('<img src="'+ newImg +'">');
        $compareBox.find('.rightImgBox').html('<img src="'+ oldImg +'">');
        $compareBox.removeClass().addClass('showImgBox');
        $('html, body').addClass('hideScroll');
    };
    
    compare.code = function(){
        return $codeCompare.mergely('get', 'lhs');
    };
    
    return compare;
    
});

/*===== mstoreDev/build#fileRes:0 =====*/

$$('fileRes', function(){
    
    var $fileRes;
    
    function fileRes(){
        
    }
    
    fileRes.getByButton = function($button){
        return $fileRes = new FileRes($button.parents('.fileResItem'));
    };
    
    fileRes.getCurrent = function(){
        return $fileRes;
    };
    
    function FileRes($fileResItem){
        var that = this;
        that.dom = $fileResItem;
        that.type = $fileResItem.attr('data-type');
        that.path = $fileResItem.attr('data-path');
        if(that.type == 'resFile'){
            that.filePath = $fileResItem.attr('data-filepath');  
            that.isResFile = true;
        }
    }
    
    FileRes.prototype.submit = function(dataFrom){
        
        var that = this;
        var requestData;
        
        if(dataFrom == 'compareCode'){
            requestData = that.getRequestData({
                cmd: 'fileRes_submit',
                code: $$.compare.code(),
                isCode: true
            });
        }else{
            requestData = that.getRequestData('fileRes_submit');
        }
        
        if(dataFrom == 'allFileRes'){
            submit();
        }else{
            layer.confirm('你确定要提交吗？', function(index){
			    submit();
    			layer.close(index);
            });
        }
        
        function submit(){
            $util.ajax(function(resp){
				if(resp.err){
					layer.alert(resp.err);
				}else{
					$$.compare.close();
					that.rebuild();
				}
			}, requestData);
        }
        
    };
    
    FileRes.prototype.rebuild = function(){
        var that = this;
        that.setStatus('build');
        $util.ajax(function(resp){
			if(resp.err){
				//layer.alert(resp.err);
				that.setStatus('error', resp.err);
			}else{
			    that.setMs(resp.ms);
				if(resp.isChange){
				    that.setStatus('change');
				}else{
				    that.setStatus('skip');
				}
			}
        }, that.getRequestData('fileRes_build'));
    };
    
    FileRes.prototype.compare = function(){
        var that = this;
        
        //return $$.compare.showImg('resp.newImg.jpg', 'resp.oldImg.jpg');
        //return $$.compare.showCode('resp.newCode\n\ngjhghg\n\nhjhj\n\n', 'resp.oldCode\n\nhjhjh\n\njhjhj\n\njgjgg', 'ftl');
        
        $util.ajax(function(resp){
            if(resp.err){
                layer.alert(resp.err);
            }else{
                if(resp.dataType == 'img'){
                    $$.compare.showImg(resp.newImg, resp.oldImg);
                }else if(resp.dataType == 'code'){
                    $$.compare.showCode(resp.newCode, resp.oldCode, resp.dataMode);
                }
            }
        }, that.getRequestData('fileRes_compare'));
    };
    
    FileRes.prototype.getRequestData = function(oData){
        var that = this;
        var requestData = {
            type: that.type,
            path: that.path
        };
        if(that.isResFile){
            requestData.filePath = that.filePath;
        }
        if(typeof oData == 'string'){
            oData = {
                cmd: oData
            };
        }
        return $util.mix(requestData, oData);
    };
    
    FileRes.prototype.setStatus = function(type, msg){
        this.dom.removeClass().addClass('fileResItem fc6 status-' + type);
        if(type == 'error'){
            this.dom.find('.statusError').text(msg);
        }
    };
    
    FileRes.prototype.setMs = function(ms){
        this.dom.find('.costTime').text(' ... ' + ms + 'ms');
    };
    
    return fileRes;
    
});

/*===== mstoreDev/build#pagerConfig:0 =====*/

$$(function($it){
    
    $it.click({
        viewMore: function(){
            if($(this).hasClass('open')){
                $(this).removeClass('open').text('点击展开').next('.noChangeList').slideUp();
            }else{
                $(this).addClass('open').text('点击收起').next('.noChangeList').slideDown();
            }
        },
        rebuildFile: function(){
            $$.fileRes.getByButton($(this)).rebuild();
        },
        compareFile: function(){
            $$.fileRes.getByButton($(this)).compare();
        },
        submitFile: function(){
            $$.fileRes.getByButton($(this)).submit();
        },
        submitAll: function(){
            $(this).parents('.changeItem').find('.status-change [jhand="submitFile"]').each(function(){
                //console.info($(this));
                $$.fileRes.getByButton($(this)).submit('allFileRes');
			});
        }
    });
    
});
